package com.cpecc.controller.wuzi;

import com.cpecc.framework.web.APIResult;
import com.cpecc.liucheng.manager.HuanJieManager;
import com.cpecc.liucheng.manager.LiuChengManager;
import com.cpecc.mail.SendMailThread;
import com.cpecc.mail.SendMailThread1;
import com.cpecc.manager.system.*;
import com.cpecc.manager.wuzi.*;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.FenTan;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.YiJiao;
import com.cpecc.model.wuzi.YiJiaoJL;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/wuzi")
public class YiJiaoController {

	@Autowired
	private UserManager userManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@Autowired
	private DepManager depManager;
	
	@Autowired
	private HuanJieManager huanJieManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private YiJiaoJLManager yiJiaoJLManager;
	
	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	@Autowired
	private FenTanManager fenTanManager;
	
	@Autowired
	private EmailManager emailManager;
	
	@Autowired
	private ToDoManager toDoManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private BuPeiManager buPeiManager;
	
	@Autowired
	private YouJianManager youJianManager;
	
	@Autowired
	private RootManager rootManager;

	@Autowired
	private LiuChengManager liuChengManager;
	
	private List<User> getXiaYiBuR(String yiJiaoId){
		List<User> list=new ArrayList<>();
		YiJiao yiJiao=yiJiaoManager.get(yiJiaoId);
		int huanJie=yiJiao.getHuanJie().getId();
		switch(huanJie){
			//低值管理员审批
			case 502: 
			//移交待发放
			case 503: list.addAll(userManager.getGZGLY()); break;
			//新使用人验收
			case 504: list.add(yiJiao.getWuZi().getUser()); break;
			case 505: list.addAll(userManager.getGZGLY()); break;

			case 507:
			case 508:
			case 509:
				list.addAll(liuChengManager.listUser(yiJiao.getLiuChengId(), huanJie));
				break;

			default:
				break;
		}
		return list;
	}
	
	/*
	 * 移交申请
	 */
	@RequestMapping(value = "/yiJiaoSQ")
	public ModelAndView yiJiaoSQ(String id) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("wuZi", wuZiManager.get(id));
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("yijiao/yiJiao");
		return mav;
	}
	
	@RequestMapping(value = "/yiJiaoSave")
	public void yiJiaoSave(String wuZiId,String xinCunFangD,String depId,String userId,String yiJiaoYY,HttpServletResponse response){
		if(weiXiuManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}			
		else if(yiJiaoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else if(baoFeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(huanHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(tuiHuoManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}else if(buPeiManager.hasWuZi(wuZiId)){
			response.setContentType("application/json");
			String result="{\"result\":\"conflict\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		WuZi wuZi=wuZiManager.get(wuZiId);

		YiJiao yiJiao=new YiJiao();
		yiJiao.setBuBan(0);
		//yiJiao.setHuanJie(huanJieManager.get(502));
		yiJiao.setHuanJie(huanJieManager.get(507));
		yiJiao.setJieShouDW(depManager.get(depId));
		yiJiao.setJieShouR(userManager.getUser(userId));
		yiJiao.setLiuChengLX(2);
		yiJiao.setShenQingRQ(new Date());
		yiJiao.setWuZi(wuZi);
		yiJiao.setYiJiaoR(user);
		yiJiao.setYiJiaoYY(yiJiaoYY);
		yiJiao.setLiuChengId(System.currentTimeMillis() + "");
		yiJiaoManager.save(yiJiao);

		liuChengManager.save(yiJiao.getLiuChengId(), 507, userManager.getBuMenLingDaos2(user));

		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(501));
		yiJiaoJL.setPiFuLX("原使用人移交");
		yiJiaoJL.setPiFuNR("提交");
		yiJiaoJL.setUser(user);
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJLManager.save(yiJiaoJL);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(502));
			toDo.setTask("移交审批");
			toDo.setType("移交");
			toDo.setUser(user1);
			toDo.setYiJiao(yiJiao);
			toDoManager.save(toDo);
			// 发送邮件
			YouJian youJian=youJianManager.get(502);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 502, user1, youJian);
				st.setYiJiao(yiJiao);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}

		for (User userCaiWu : userManager.getAllCaiWuR()) {
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(502));
			toDo.setTask("移交通知");
			toDo.setType("移交");
			toDo.setUser(userCaiWu);
			toDo.setYiJiao(yiJiao);
			toDoManager.save(toDo);
			// 发送邮件
			YouJian youJian=youJianManager.get(502);
			if(youJian!=null && youJian.getState() == 1){
				SendMailThread st=new SendMailThread(email, root, 502, userCaiWu, youJian);
				st.setYiJiao(yiJiao);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}

		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 移交审批
	 */
	@RequestMapping(value = "/yiJiaoDaiBan")
	public ModelAndView yiJiaoDaiBan(){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiaoJieShou", yiJiaoManager.getMyYiJiaoJS(user));

		if(currentUser.hasRole("低值管理员")){
			mav.addObject("yiJiaoList", yiJiaoManager.getAllYiJiao(502));
			mav.addObject("yiJiaoFaFang", yiJiaoManager.getAllYiJiao(503));
			mav.addObject("yiJiaoRZ", yiJiaoManager.getAllYiJiao(505));
			mav.addObject("yiJiaoLiuCheng", yiJiaoManager.activeYiJiao());
		}

		if(currentUser.hasRole("财务")) {
			mav.addObject("yiJiaoCaiWu", yiJiaoManager.getAllYiJiao());
		}

		mav.addObject("yiJiaoList507", yiJiaoManager.getMyYiJiaoRW(user, 507));
		mav.addObject("yiJiaoList508", yiJiaoManager.getMyYiJiaoRW(user, 508));
		mav.addObject("yiJiaoList509", yiJiaoManager.getMyYiJiaoRW(user, 509));

		mav.setViewName("yijiao/yiJiaoDaiBan");
		return mav;
	}
	
	@RequestMapping(value = "/getYiJiaoShu")
	public void getYiJiaoShu(HttpServletResponse response){
		int zongShu=0;
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		if(currentUser.hasRole("低值管理员")){
			zongShu+=yiJiaoManager.getAllYiJiao(502).size();
			zongShu+=yiJiaoManager.getAllYiJiao(503).size();
			zongShu+=yiJiaoManager.getAllYiJiao(505).size();
		}
		zongShu+=yiJiaoManager.getMyYiJiaoJS(user).size();
		if(currentUser.hasRole("财务")) {
			zongShu += yiJiaoManager.getAllYiJiao().size();
		}

		zongShu += yiJiaoManager.getMyYiJiaoRW(user, 507).size();
		zongShu += yiJiaoManager.getMyYiJiaoRW(user, 508).size();
		zongShu += yiJiaoManager.getMyYiJiaoRW(user, 509).size();

		response.setContentType("application/json;charset=utf-8");		
		String result="{\"result\":\"("+zongShu+")\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * TODO yijiao LiuCheng
	 * @return
	 */
	@RequestMapping(value = "/yijiao/liucheng/tongguo", method = RequestMethod.POST)
	@ResponseBody
	public APIResult weiXiuLiuCheng(String yiJiaoId) {
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);

		yiJiaoManager.doLiuCheng(yiJiaoId, user);

		return APIResult.success();
	}
	
	@RequestMapping(value = "/yjspList")
	public ModelAndView yjspList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiaoList", yiJiaoManager.getAllYiJiao(502));
		mav.setViewName("yijiao/yjspListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/yiJiaoRK")
	public void yiJiaoRK(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		YiJiao yiJiao=yiJiaoManager.get(id);
		yiJiao.setHuanJie(huanJieManager.get(503));
		yiJiaoManager.cover(yiJiao);

		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(502));
		yiJiaoJL.setPiFuLX("移交入库");
		yiJiaoJL.setPiFuNR("接收");
		yiJiaoJL.setUser(user);
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJLManager.save(yiJiaoJL);

		WuZi wuZi=yiJiao.getWuZi();
		fenTanManager.quChuFenTan(wuZi, new Date());
		wuZi.setShiYongZT(shiYongZTManager.get(105));
		wuZi.setUser(user);
		wuZi.setDep(user.getDep());		
		wuZi.setGuanLiBM(user.getDep());
		wuZiManager.cover(wuZi);
		
		toDoManager.deleteList(yiJiao);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(503));
			toDo.setTask("移交发放");
			toDo.setType("移交");
			toDo.setUser(user1);
			toDo.setYiJiao(yiJiao);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(503);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 503, user1, youJian);
				st.setYiJiao(yiJiao);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/yjspPiSubmit")
	public void yjspPiSubmit(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String type[]=request.getParameterValues("yjspId");
		for(int i=0;i<type.length;i++){
			YiJiao yiJiao=yiJiaoManager.get(type[i]);
			yiJiao.setHuanJie(huanJieManager.get(503));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(502));
			yiJiaoJL.setPiFuLX("移交入库");
			yiJiaoJL.setPiFuNR("接收");
			yiJiaoJL.setUser(user);
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJLManager.save(yiJiaoJL);
			WuZi wuZi=yiJiao.getWuZi();
			fenTanManager.quChuFenTan(wuZi, new Date());
			wuZi.setShiYongZT(shiYongZTManager.get(105));
			wuZi.setUser(user);
			wuZi.setDep(user.getDep());		
			wuZi.setGuanLiBM(user.getDep());
			wuZiManager.cover(wuZi);
			Email email=emailManager.getEmail();
			String root=rootManager.getRoot();
			toDoManager.deleteList(yiJiao);
			for(User user1:userManager.getGZGLY()){
				ToDo toDo=new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setHuanJie(huanJieManager.get(503));
				toDo.setTask("移交发放");
				toDo.setType("移交");
				toDo.setUser(user1);
				toDo.setYiJiao(yiJiao);
				toDoManager.save(toDo);
				//\\发送邮件
				YouJian youJian=youJianManager.get(503);
				if(youJian!=null && youJian.getState()==1){
					SendMailThread st=new SendMailThread(email, root, 503, user1, youJian);
					st.setYiJiao(yiJiao);
					st.setWuZi(wuZi);
					Thread t=new Thread(st);
					t.start();
				}
			}
		}		
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
		}
	}
	
	@RequestMapping(value = "/yiJiaoBoHui")
	public ModelAndView yiJiaoBoHuiSQ(String id) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiaoId", id);
		mav.setViewName("yijiao/yiJiaoBoHui");
		return mav;
	}
	
	@RequestMapping(value = "/yiJiaoBoHuiTJ")
	public void yiJiaoBoHuiTJ(String yiJiaoId,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		YiJiao yiJiao=yiJiaoManager.get(yiJiaoId);
		int dangQianHuanJie=yiJiao.getHuanJie().getId();
		yiJiao.setHuanJie(huanJieManager.get(506));
		yiJiaoManager.cover(yiJiao);
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
		yiJiaoJL.setPiFuLX("移交退回");
		yiJiaoJL.setPiFuNR(boHuiYY);
		yiJiaoJL.setUser(user);
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJLManager.save(yiJiaoJL);
		//移交驳回
		toDoManager.deleteList(yiJiao);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		if(yiJiao.getBuBan()==1){
			//是补办移交
			WuZi wuZi=yiJiao.getWuZi();
			if(wuZi.getPanDian()==3){
				wuZi.setPanDian(1);
				wuZiManager.cover(wuZi);
			}
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setTask("资产盘点");
			toDo.setType("资产盘点");
			toDo.setUser(wuZi.getUser());
			toDo.setWuZi(wuZi);
			toDoManager.save(toDo);
			//\\发送邮件(盘点)
			YouJian youJian=youJianManager.get(3);
			SendMailThread1 st=new SendMailThread1(email, wuZi.getUser(), youJian, wuZi);
			st.setRoot(root);
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/yjspPL")
	public void yjspPL(HttpServletRequest request,HttpServletResponse response) {
		String type[]=request.getParameterValues("yjspId");
		String result="{\"result\":\"none\"}";
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/yjBHPL")
	public ModelAndView yjBHPL(HttpServletRequest request) {
		ModelAndView mav=new ModelAndView();
		String type[]=request.getParameterValues("yjspId");
		if(type!=null){
			String yjspIds="";
			for(int i=0;i<type.length;i++){
				yjspIds=yjspIds+type[i]+",";
			}
			if(yjspIds.length()>0)
				yjspIds=yjspIds.substring(0, yjspIds.length()-1);
			mav.addObject("yjspIds", yjspIds);
		}
		mav.setViewName("yijiao/yjBHPL");
		return mav;
	}
	
	@RequestMapping(value = "/yjBHPLSave")
	public void yjBHPLSave(String yjspIds,String boHuiYY,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		String yjspId[]=yjspIds.split(",");
		for(int i=0;i<yjspId.length;i++){
			YiJiao yiJiao=yiJiaoManager.get(yjspId[i]);
			int dangQianHuanJie=yiJiao.getHuanJie().getId();
			yiJiao.setHuanJie(huanJieManager.get(506));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
			yiJiaoJL.setPiFuLX("移交退回");
			yiJiaoJL.setPiFuNR(boHuiYY);
			yiJiaoJL.setUser(user);
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJLManager.save(yiJiaoJL);
			//移交驳回
			toDoManager.deleteList(yiJiao);
			if(yiJiao.getBuBan()==1){
				//是补办移交
				WuZi wuZi=yiJiao.getWuZi();
				if(wuZi.getPanDian()==3){
					wuZi.setPanDian(1);
					wuZiManager.cover(wuZi);
				}
				ToDo toDo=new ToDo();
				toDo.setCreateDate(new Date());
				toDo.setTask("资产盘点");
				toDo.setType("资产盘点");
				toDo.setUser(wuZi.getUser());
				toDo.setWuZi(wuZi);
				toDoManager.save(toDo);
				//\\发送邮件(盘点)
				YouJian youJian=youJianManager.get(3);
				SendMailThread1 st=new SendMailThread1(email, wuZi.getUser(), youJian, wuZi);
				st.setRoot(root);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 移交发放
	 */
	@RequestMapping(value = "/yjffList")
	public ModelAndView yjffList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiaoFaFang", yiJiaoManager.getAllYiJiao(503));
		mav.setViewName("yijiao/yjffListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/yiJiaoFaFang")
	public ModelAndView yiJiaofaFang(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiao", yiJiaoManager.get(id));
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("yijiao/yiJiaoFaFang");
		return mav;
	}
	
	@RequestMapping(value = "/yiJiaoFaFangSave")
	public void yiJiaoFaFangSave(String yiJiaoId,String shiYongdepId,String shiYonguserId,String cunFangD,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		YiJiao yiJiao=yiJiaoManager.get(yiJiaoId);
		yiJiao.setHuanJie(huanJieManager.get(504));
		yiJiaoManager.cover(yiJiao);
		WuZi wuZi=yiJiao.getWuZi();		
		wuZi.setDep(depManager.get(shiYongdepId));
		User shiYongR = userManager.getUser(shiYonguserId);
		wuZi.setUser(shiYongR);
		wuZi.setGuanLiBM(depManager.get(shiYongdepId));
		wuZi.setCunFangD(cunFangD);
		wuZiManager.cover(wuZi);
		List<FenTan> fenTanList=fenTanManager.getFenTanBM(wuZi);
		float bili=0f;
		for(FenTan t:fenTanList){
			bili+=t.getPercent();			
		}
		if(bili>0 && bili<100){
			response.setContentType("application/json");
			String result="{\"result\":\"bili\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else if(bili==0){
			FenTan fenTan=new FenTan();
			fenTan.setDep(depManager.get(shiYongdepId));
			fenTan.setZengJiaRQ(new Date());
			fenTan.setWuZi(wuZi);
			fenTan.setFenTanZT(1);
			fenTan.setPercent(100f);
			fenTanManager.save(fenTan);
		}
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(503));
		yiJiaoJL.setPiFuLX("移交发放");
		yiJiaoJL.setPiFuNR("发放");
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJL.setUser(user);
		yiJiaoJLManager.save(yiJiaoJL);
		toDoManager.deleteList(yiJiao);
		ToDo toDo=new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(504));
		toDo.setTask("移交接收");
		toDo.setType("移交");
		toDo.setUser(shiYongR);
		toDo.setYiJiao(yiJiao);
		toDoManager.save(toDo);
		//\\发送邮件
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		YouJian youJian=youJianManager.get(504);
		if(youJian!=null && youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 504, shiYongR, youJian);
			st.setYiJiao(yiJiao);
			st.setWuZi(wuZi);
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/kuCunFaFang")
	public ModelAndView kuCunFaFang(String id){
		ModelAndView mav=new ModelAndView();
		mav.addObject("wuZi", wuZiManager.get(id));
		mav.addObject("depList", depManager.getAllDep());
		mav.setViewName("yijiao/kuCunFaFang");
		return mav;
	}
	
	@RequestMapping(value = "/kuCunFaFangSave")
	public void kuCunFaFangSave(String wuZiId,String depId2,String userId,String cunFangD,HttpServletResponse response){
		WuZi wuZi=wuZiManager.get(wuZiId);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		YiJiao yiJiao=null;
		if(yiJiaoManager.getYiJiaoByWuZi(wuZi,503)!=null){
			yiJiao=yiJiaoManager.getYiJiaoByWuZi(wuZi,503);
			yiJiao.setHuanJie(huanJieManager.get(504));
			yiJiaoManager.cover(yiJiao);
		}else{
			yiJiao=new YiJiao();
			yiJiao.setWuZi(wuZi);
			yiJiao.setHuanJie(huanJieManager.get(504));
			yiJiao.setJieShouDW(depManager.get(depId2));
			yiJiao.setJieShouR(userManager.getUser(userId));
			yiJiao.setLiuChengLX(2);
			yiJiao.setShenQingRQ(new Date());
			yiJiao.setYiJiaoR(user);
			yiJiao.setYiJiaoYY("库存发放");		
			yiJiaoManager.save(yiJiao);
		}
		wuZi.setDep(depManager.get(depId2));
		wuZi.setUser(userManager.getUser(userId));
		wuZi.setCunFangD(cunFangD);
		wuZi.setShiYongZT(shiYongZTManager.get(102));
		wuZiManager.cover(wuZi);
		
		List<FenTan> fenTanList=fenTanManager.getFenTanBM(wuZi);
		float bili=0f;
		for(FenTan t:fenTanList){
			bili+=t.getPercent();			
		}
		if(bili>0 && bili<100){
			response.setContentType("application/json");
			String result="{\"result\":\"bili\"}";
			try {
				response.getWriter().write(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		else if(bili==0){
			FenTan fenTan=new FenTan();
			fenTan.setDep(depManager.get(depId2));
			fenTan.setZengJiaRQ(new Date());
			fenTan.setWuZi(wuZi);
			fenTan.setFenTanZT(1);
			fenTan.setPercent(100f);
			fenTanManager.save(fenTan);
		}
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(503));
		yiJiaoJL.setPiFuLX("库存发放");
		yiJiaoJL.setPiFuNR("发放");
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJL.setUser(user);
		yiJiaoJLManager.save(yiJiaoJL);
		
		toDoManager.deleteList(yiJiao);
		ToDo toDo=new ToDo();
		toDo.setCreateDate(new Date());
		toDo.setHuanJie(huanJieManager.get(504));
		toDo.setTask("移交接收");
		toDo.setType("移交");
		toDo.setUser(userManager.getUser(userId));
		toDo.setYiJiao(yiJiao);
		toDoManager.save(toDo);
		//\\发送邮件
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		YouJian youJian=youJianManager.get(504);
		if(youJian!=null && youJian.getState()==1){
			SendMailThread st=new SendMailThread(email, root, 504, userManager.getUser(userId), youJian);
			st.setYiJiao(yiJiao);
			st.setWuZi(wuZi);
			Thread t=new Thread(st);
			t.start();
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 移交接收
	 */
	@RequestMapping(value = "/yjjsList")
	public ModelAndView yjjsList(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("yiJiaoJieShou", yiJiaoManager.getMyYiJiaoJS(user));
		mav.setViewName("yijiao/yjjsListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/yiJiaoJieShou")
	public void yiJiaoJieShou(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		YiJiao yiJiao=yiJiaoManager.get(id);
		yiJiao.setHuanJie(huanJieManager.get(505));
		yiJiaoManager.cover(yiJiao);
		WuZi wuZi=yiJiao.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(102));
		wuZiManager.cover(wuZi);
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(504));
		yiJiaoJL.setPiFuLX("新使用人接收");
		yiJiaoJL.setPiFuNR("接收");
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJL.setUser(user);
		yiJiaoJLManager.save(yiJiaoJL);
		if(wuZi.getPanDian()==3){
			wuZi.setPanDian(1);
			wuZiManager.cover(wuZi);
		}
		toDoManager.deleteList(yiJiao);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setCreateDate(new Date());
			toDo.setHuanJie(huanJieManager.get(505));
			toDo.setTask("移交结果确认");
			toDo.setType("移交");
			toDo.setUser(user1);
			toDo.setYiJiao(yiJiao);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(505);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 505, user1, youJian);
				st.setYiJiao(yiJiao);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	
	@RequestMapping(value = "/yjjsPL")
	public void yjjsPL(HttpServletRequest request,HttpServletResponse response){
		String result="{\"result\":\"none\"}";
		String type[]=request.getParameterValues("yjjsId");
		if(type!=null){
			result="{\"result\":\"success\"}";
		}
		response.setContentType("application/json");
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/yjjsPLTY")
	public void yjjsPLTY(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String type[]=request.getParameterValues("yjjsId");
		if(type!=null){
			Email email=emailManager.getEmail();
			String root=rootManager.getRoot();
			for(int i=0;i<type.length;i++){
				YiJiao yiJiao=yiJiaoManager.get(type[i]);
				yiJiao.setHuanJie(huanJieManager.get(505));
				yiJiaoManager.cover(yiJiao);
				WuZi wuZi=yiJiao.getWuZi();
				wuZi.setShiYongZT(shiYongZTManager.get(102));
				wuZiManager.cover(wuZi);
				YiJiaoJL yiJiaoJL=new YiJiaoJL();
				yiJiaoJL.setCaoZuoRQ(new Date());
				yiJiaoJL.setHuanJie(huanJieManager.get(504));
				yiJiaoJL.setPiFuLX("新使用人接收");
				yiJiaoJL.setPiFuNR("接收");
				yiJiaoJL.setYiJiao(yiJiao);
				yiJiaoJL.setUser(user);
				yiJiaoJLManager.save(yiJiaoJL);
				if(wuZi.getPanDian()==3){
					wuZi.setPanDian(1);
					wuZiManager.cover(wuZi);
				}
				toDoManager.deleteList(yiJiao);
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieManager.get(505));
					toDo.setTask("移交结果确认");
					toDo.setType("移交");
					toDo.setUser(user1);
					toDo.setYiJiao(yiJiao);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(505);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 505, user1, youJian);
						st.setYiJiao(yiJiao);
						st.setWuZi(wuZi);
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/yiJiaoTuiHuiSQ")
	public ModelAndView yiJiaoTuiHuiSQ(String id) {
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiaoId", id);
		mav.setViewName("yijiao/yiJiaoTuiHui");
		return mav;
	}
	
	@RequestMapping(value = "/yiJiaoTuiHuiSave")
	public void yiJiaoTuiHuiTJ(String yjId,String tuiHuiYY,HttpServletResponse response){
		YiJiao yiJiao=yiJiaoManager.get(yjId);
		int dangQianHuanJie=yiJiao.getHuanJie().getId();
		yiJiao.setHuanJie(huanJieManager.get(502));
		yiJiaoManager.cover(yiJiao);
		WuZi wuZi=yiJiao.getWuZi();
		User u=yiJiao.getYiJiaoR();
		wuZi.setUser(u);
		wuZi.setDep(u.getDep());
		wuZiManager.cover(wuZi);
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
		yiJiaoJL.setPiFuLX("移交退回");
		yiJiaoJL.setPiFuNR(tuiHuiYY);
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		yiJiaoJL.setUser(user);
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJLManager.save(yiJiaoJL);
		toDoManager.deleteList(yiJiao);
		Email email=emailManager.getEmail();
		String root=rootManager.getRoot();
		//低值管理员
		for(User user1:userManager.getGZGLY()){
			ToDo toDo=new ToDo();
			toDo.setUser(user1);
			toDo.setHuanJie(huanJieManager.get(502));
			toDo.setCreateDate(new Date());
			toDo.setTask("移交审批");
			toDo.setType("移交");
			toDo.setYiJiao(yiJiao);
			toDoManager.save(toDo);
			//\\发送邮件
			YouJian youJian=youJianManager.get(502);
			if(youJian!=null && youJian.getState()==1){
				SendMailThread st=new SendMailThread(email, root, 502, user1, youJian);
				st.setYiJiao(yiJiao);
				st.setWuZi(wuZi);
				Thread t=new Thread(st);
				t.start();
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/yjTHPL")
	public ModelAndView yjTHPL(HttpServletRequest request) {
		ModelAndView mav=new ModelAndView();
		String type[]=request.getParameterValues("yjjsId");
		if(type!=null){
			String yjjsIds="";
			for(int i=0;i<type.length;i++){
				yjjsIds=yjjsIds+type[i]+",";
			}
			if(yjjsIds.length()>0)
				yjjsIds=yjjsIds.substring(0,yjjsIds.length()-1);
			mav.addObject("yjjsIds", yjjsIds);
		}
		mav.setViewName("yijiao/yjTHPL");
		return mav;
	}
	
	@RequestMapping(value = "/yjTHPLSave")
	public void yjTHPLSave(String yjIds,String tuiHuiYY,HttpServletResponse response){
		String yjId[]=yjIds.split(",");
		if(yjId!=null){
			Subject currentUser = SecurityUtils.getSubject();
			Object obj=currentUser.getPrincipals();
			String name=obj.toString();
			User user=userManager.findByUsername(name);
			Email email=emailManager.getEmail();
			String root=rootManager.getRoot();
			for(int i=0;i<yjId.length;i++){
				YiJiao yiJiao=yiJiaoManager.get(yjId[i]);
				int dangQianHuanJie=yiJiao.getHuanJie().getId();
				yiJiao.setHuanJie(huanJieManager.get(502));
				yiJiaoManager.cover(yiJiao);
				WuZi wuZi=yiJiao.getWuZi();
				User u=yiJiao.getYiJiaoR();
				wuZi.setUser(u);
				wuZi.setDep(u.getDep());
				wuZiManager.cover(wuZi);
				YiJiaoJL yiJiaoJL=new YiJiaoJL();
				yiJiaoJL.setCaoZuoRQ(new Date());
				yiJiaoJL.setHuanJie(huanJieManager.get(dangQianHuanJie));
				yiJiaoJL.setPiFuLX("移交退回");
				yiJiaoJL.setPiFuNR(tuiHuiYY);
				yiJiaoJL.setUser(user);
				yiJiaoJL.setYiJiao(yiJiao);
				yiJiaoJLManager.save(yiJiaoJL);
				toDoManager.deleteList(yiJiao);
				//低值管理员
				for(User user1:userManager.getGZGLY()){
					ToDo toDo=new ToDo();
					toDo.setUser(user1);
					toDo.setHuanJie(huanJieManager.get(502));
					toDo.setCreateDate(new Date());
					toDo.setTask("移交审批");
					toDo.setType("移交");
					toDo.setYiJiao(yiJiao);
					toDoManager.save(toDo);
					//\\发送邮件
					YouJian youJian=youJianManager.get(502);
					if(youJian!=null && youJian.getState()==1){
						SendMailThread st=new SendMailThread(email, root, 502, user1, youJian);
						st.setYiJiao(yiJiao);
						st.setWuZi(wuZi);
						Thread t=new Thread(st);
						t.start();
					}
				}
			}
		}
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 移交入账
	 */
	@RequestMapping(value = "/yjrzList")
	public ModelAndView yjrzList(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiaoRZ", yiJiaoManager.getAllYiJiao(505));
		mav.setViewName("yijiao/yjrzListAjax");
		return mav;
	}
	
	@RequestMapping(value = "/yiJiaoRZ")
	public void yiJiaoRZ(String id,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		YiJiao yiJiao=yiJiaoManager.get(id);
		yiJiao.setHuanJie(huanJieManager.get(506));
		yiJiaoManager.cover(yiJiao);
		YiJiaoJL yiJiaoJL=new YiJiaoJL();
		yiJiaoJL.setCaoZuoRQ(new Date());
		yiJiaoJL.setHuanJie(huanJieManager.get(505));
		yiJiaoJL.setPiFuLX("移交确认");
		yiJiaoJL.setPiFuNR("移交确认");
		yiJiaoJL.setYiJiao(yiJiao);
		yiJiaoJL.setUser(user);
		yiJiaoJLManager.save(yiJiaoJL);
		toDoManager.deleteList(yiJiao);
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/yjrzPiSubmit")
	public void yjrzPiSubmit(HttpServletRequest request,HttpServletResponse response){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		String type[]=request.getParameterValues("yjrzId");
		for(int i=0;i<type.length;i++){
			YiJiao yiJiao=yiJiaoManager.get(type[i]);
			yiJiao.setHuanJie(huanJieManager.get(506));
			yiJiaoManager.cover(yiJiao);
			YiJiaoJL yiJiaoJL=new YiJiaoJL();
			yiJiaoJL.setCaoZuoRQ(new Date());
			yiJiaoJL.setHuanJie(huanJieManager.get(505));
			yiJiaoJL.setPiFuLX("移交确认");
			yiJiaoJL.setPiFuNR("移交确认");
			yiJiaoJL.setYiJiao(yiJiao);
			yiJiaoJL.setUser(user);
			yiJiaoJLManager.save(yiJiaoJL);
			
			toDoManager.deleteList(yiJiao);
		}		
		response.setContentType("application/json");
		String result="{\"result\":\"success\"}";
		try {
			response.getWriter().write(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(value = "/yiJiaoDetail")
	public ModelAndView yiJiaoDetail(String id){
		YiJiao yiJiao=yiJiaoManager.get(id);
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiao", yiJiao);
		mav.addObject("xiaYiBuR", getXiaYiBuR(id));
		mav.addObject("yiJiaoJLList", yiJiaoJLManager.getAllJL(id));
		mav.setViewName("yijiao/yiJiaoDetail");
		return mav;
	}
	
	@RequestMapping(value = "/myYiJiaoSP")
	public ModelAndView getMyYiJiaoSP(){
		ModelAndView mav=new ModelAndView();
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		mav.addObject("yiJiaoSPList", yiJiaoJLManager.getMyYJSPJL(user));
		mav.setViewName("yijiao/myYiJiaoSP");
		return mav;
	}
	
	@RequestMapping(value = "/myYiJiao")
	public ModelAndView myYiJiao(){
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		ModelAndView mav=new ModelAndView();
		mav.addObject("yiJiaoList", yiJiaoManager.getMyYiJiao(user));
		mav.setViewName("yijiao/myYiJiao");
		return mav;
	}
}
